



console.log('111111111111');


module.exports=function(){

    
    // 所有事件
    var events = {}

    return {

        // 添加事件
        on: function (event_name, handles) {

            var event_names = event_name.split(".");
            var old, parent;

            old = parent = events

           // 遍历事件名，给最后一个事件添加回调
           for (var i = 0, len = event_names.length; i < len; i++) {

                var _event = event_names[i];
                var event_val = parent[_event]

                if (!event_val) {
                    
                    if(i ==len-1){
                        parent[_event]=handles
                    }else{
                        parent[_event] = {}
                    }
                }

                // 如果事件的值是数组，那么这个数组就是回调数组
                else if (event_val instanceof Array) {
                    if (handles instanceof Function)
                        event_val.push(handles)
                }

                // 如果事件值是函数，那么把值初始化为数组，然后把原回调放进去
                else if (event_val instanceof Function) {
                    parent[_event] = [event_val,handles]
                }

                old = parent
                parent = parent[_event]
            }


        },

        // 触发事件
        emit: function () {


            var args =Array.prototype.slice.call(arguments)
            var _event = args[0]
            var data  =args.slice(1);
            var event_names = _event.split(".")
            var old, parent;

            old = parent = events;

            console.log(_event,args)
            
            for (var i = 0, len = event_names.length; i < len; i++) {
                
                var _event = event_names[i];
                var event_val = parent[_event];

                if(!event_val){
                    throw Error("不存在事件：",_event)
                }else if (event_val instanceof Function) {
                        event_val[j].apply(null,data)
                }else if(event_val instanceof Array){
                    
                    for (var j = 0, jlen = event_val.length; j < len; j++) {
                        event_val[j].apply(null,data)
                    }
                }

                old = parent
                parent = parent[_event]
            }
        },

        // 移除事件
        remove: function () {

            var args = Array.prototype.slice.call(arguments)
            var _event = args[0]
            var event_names = _event.split(".")
            var old, parent;

            old = parent = events;

            for (var i = 0, len = event_names.length; i < len; i++) {

                var _event = event_names[i];
                var event_val = parent[_event];

                if (!event_val) {
                    throw Error("不存在事件：", _event)

                } else {
                    if (i == len - 1) {
                        delete parent[_event]
                    }
                }

                old = parent
                parent = parent[_event]

            }

            console.log(events)
        }
    }

}